package bookworm;

import java.util.LinkedList;

public class CreateBooksBasedOnReviews 
{
	public static LinkedList<Book> createBooksFromReviews(LinkedList<Review> bookReviews)
	{
		LinkedList<Book> booksFromReviews = new LinkedList<Book>();
		for(int i = 0; i < bookReviews.size(); i++)
		{
			Review oneReview = bookReviews.get(i);
			String bookTitleFromOneReview = oneReview.getBookTitle();
			boolean bookWasAlreadyCreated = false;
			
			for(int j = 0; j < booksFromReviews.size(); j++)
			{
				Book oneBook = booksFromReviews.get(j);
				String oneBookTitle = oneBook.getBookName();
				if(bookTitleFromOneReview.compareTo(oneBookTitle) == 0)
				{
					bookWasAlreadyCreated = true;
					//the book was already created. So let's just add one more review to it
					oneBook.addNewReview(oneReview);
					break;
				}
			}
			
			if(bookWasAlreadyCreated == false)
			{
				//a new book must be created
				LinkedList<String> authorsFromReview = oneReview.getAuthors();
				String bookTitle = oneReview.getBookTitle();
				String urlToCover = oneReview.getMediumCover();
				Book newBook = new Book(bookTitle, authorsFromReview, urlToCover);
				newBook.addNewReview(oneReview);
				booksFromReviews.add(newBook);
			}
		}
		return booksFromReviews;
	}
}
